package com.zbvc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {

	/*
	 * JDBC连接数据库的步骤
	 * 1�? 注册驱动
	 * 2）创建连�?
	 * 3）创建语句对�?
	 * 4）执行sql操作
	 * 5）处理结果集
	 */
	// 定义三个成员变量
	private static String url="jdbc:mysql://localhost:3306/zbvc?allowPublicKeyRetrieval=true&serverTimezone=UTC&characterEncoding=utf8";
	private static String username = "root";  // 
	private static String password  ="root"; // 密码
	static { // 静态块
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			//Class.forName("com.mysql.cj.jdbc.Driver");  mysql8.0
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	}
	// 创建连接方法
	public static Connection getConnect() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url, username, password);
			return conn;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
		
		
	}
	public static void main(String[] args) {
		//测试查询user表所有数数据
		Connection c = DBUtil.getConnect();
		//System.out.println(c);    
		// 语句对象 Statement  PreparedStatement  防止SQL注入
		//String sql = " SELECT * FROM user ";  // 查询
		String sql = " INSERT INTO user (id,name,age) VALUES (?,?,?) "; // 增加
		
		try {
			PreparedStatement pstm = c.prepareStatement(sql); // 创建语句对象
			//ResultSet rs = pstm.executeQuery();  // 接受结果�?
			pstm.setInt(1, 1004);
			pstm.setString(2, "唐三");
			pstm.setInt(3, 20);
			int row = pstm.executeUpdate(); 
			System.out.println(row);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
